extends Area2D



@export var required_hits = 50  # 需要攻击次数
var type = TYPE.ATTACK_SPEED     # 道具类型
var is_unlocked = false          # 是否可获取

enum TYPE { ATTACK_SPEED, ATTACK_DAMAGE , DUPLICATE_PLAYER,ATTACK_SPEED_SUPER,ATTACK_DAMAGE_SUPER }

func _ready():
	print($"../ItemManager".currentUpCount)
	if $"../ItemManager".currentUpCount % 4 == 0 and $"../ItemManager".currentUpCount <= 20 and $"../ItemManager".currentUpCount >= 4:
		type = TYPE.DUPLICATE_PLAYER
	elif $"../ItemManager".currentUpCount == 1 or $"../ItemManager".currentUpCount == 4:
		type = TYPE.DUPLICATE_PLAYER
	elif $"../ItemManager".currentUpCount == 25:
		type = TYPE.ATTACK_SPEED_SUPER
	elif $"../ItemManager".currentUpCount == 40:
		type = TYPE.ATTACK_DAMAGE_SUPER	
	else:
		randomize()
		type = randi() % 2  # 随机生成类型
	update_display()

func update_display():
	var text = ""
	match type:
		TYPE.ATTACK_SPEED:
			text = "攻速++\n需要攻击%d次" % required_hits
		TYPE.ATTACK_DAMAGE:
			text = "攻击++\n需要攻击%d次" % required_hits
		TYPE.DUPLICATE_PLAYER:
			text = "人物+1\n需要攻击%d次" % required_hits
		TYPE.ATTACK_SPEED_SUPER:
			text = "攻速*2\n需要攻击%d次" % required_hits
		TYPE.ATTACK_DAMAGE_SUPER:
			text = "攻击*2\n需要攻击%d次" % required_hits
	
	$Label.text = text
	$Sprite2D.modulate = Color.WHITE if is_unlocked else Color.GRAY

func _on_body_entered(body):
	if body.is_in_group("player_bullet"):
		required_hits-=1
	if is_unlocked && body.is_in_group("player"):
		apply_effect(body)
		queue_free()

func apply_effect(player_bullet):
	$"../ItemManager".currentUpCount += 1;
	match type:
		TYPE.ATTACK_SPEED:
			$"../PlayerManager".attack_speed *=1.2
		TYPE.ATTACK_DAMAGE:
			$"../PlayerManager".attack_damage+=100
		TYPE.DUPLICATE_PLAYER:
			$"../PlayerManager".PlayerAddOne()
		TYPE.ATTACK_SPEED_SUPER:
			$"../PlayerManager".attack_speed *=2
		TYPE.ATTACK_DAMAGE_SUPER:
			$"../PlayerManager".attack_damage*=2



func _on_area_entered(area: Area2D) -> void:
	if area.is_in_group("player_bullet"):
		required_hits-=1
		update_display()
		area.queue_free()
	if required_hits == 0:
		apply_effect(area)
		queue_free()
		$"../ItemManager".register_hit()
